package com.whfc.ms.api.sys.controller;

import com.whfc.common.enums.SysRulePlatform;
import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.JSONUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.fuum.dto.SysDeptDTO;
import com.whfc.fuum.dto.SysDeptExtraDTO;
import com.whfc.fuum.dto.SysUserDTO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.fuum.param.*;
import com.whfc.ms.api.sys.service.MsSysDeptService;
import com.whfc.ms.api.sys.service.MsSysUserService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description:
 * @author: xugcheng
 * @version: 1.0
 * @date: 2019/7/23 10:06
 */
@RestController
@RequestMapping("/ms/api/dept")
public class SysDeptController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MsSysDeptService msSysDeptService;

    @Autowired
    private MsSysUserService msSysUserService;


    @GetMapping("/list")
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser sysUser, AppDeptListParam request) {
        logger.info("组织机构列表,username:{},参数:{}", sysUser.getUsername(), request.toString());
        PageData<SysDeptDTO> pageData = msSysDeptService.list(request);
        return ResultUtil.success(pageData);
    }


    @GetMapping(value = "list", params = "ruleCode")
    public Result getDeptTree(@SessionAttribute(SessionAttr.MS_USER) SysUser sysUser,
                              @RequestParam(value = "ruleCode") String ruleCode,
                              @RequestParam(value = "deptId", required = false) Integer deptId) {
        logger.info("加载用户包含指定权限的组织树,username:{},ruleId:{}", sysUser.getUsername(), ruleCode);
        List<SysDeptDTO> deptList = msSysDeptService.getDeptListByRuleCode(sysUser, deptId, ruleCode, SysRulePlatform.MS);
        return ResultUtil.success(new ListData<>(deptList));
    }

    //@RequiresPermissions(value = {"sys/dept", "sys-gscx/dept"}, logical = Logical.OR)
    @GetMapping("tree")
    public Result getDeptTree(@SessionAttribute(SessionAttr.MS_USER) SysUser sysUser,
                              @RequestParam(value = "deptId", required = false) Integer deptId,
                              @RequestParam(value = "keyword", required = false) String keyword) {
        logger.info("加载用户的组织树,username:{},deptId:{},keyword:{}", sysUser.getUsername(), deptId, keyword);
        List<SysDeptDTO> deptTree = msSysDeptService.getDeptTree(sysUser, deptId, keyword);
        return ResultUtil.success(new ListData<>(deptTree));
    }

    @RequiresPermissions(value = {"sys/dept/add", "sys-gscx/dept/add"}, logical = Logical.OR)
    @PostMapping("add")
    public Result addDept(@SessionAttribute(SessionAttr.MS_USER) SysUser sysUser, @Validated @RequestBody SysDeptAdd deptAdd) {
        logger.info("添加组织机构,username:{},data:{}", sysUser.getUsername(), deptAdd);
        msSysDeptService.addDept(deptAdd);
        return ResultUtil.success();
    }

    @RequiresPermissions(value = {"sys/dept/edit", "sys-gscx/dept/edit"}, logical = Logical.OR)
    @PostMapping("edit")
    public Result editDept(@SessionAttribute(SessionAttr.MS_USER) SysUser sysUser, @Validated @RequestBody SysDeptEdit deptEdit) {
        logger.info("修改组织机构,username:{},data:{}", sysUser.getUsername(), deptEdit);
        msSysDeptService.editDept(deptEdit);
        return ResultUtil.success();
    }

    @RequiresPermissions(value = {"sys/dept/del", "sys-gscx/dept/del"}, logical = Logical.OR)
    @PostMapping("del/{deptId}")
    public Result delDept(@SessionAttribute(SessionAttr.MS_USER) SysUser sysUser, @PathVariable("deptId") Integer deptId) {

        logger.info("删除组织机构,username:{},deptId:{}", sysUser.getUsername(), deptId);
        msSysDeptService.delDept(deptId);
        return ResultUtil.success();
    }


    @RequiresPermissions(value = {"sys/deptUser/edit", "sys-gscx/deptUser/edit"}, logical = Logical.OR)
    @PostMapping("/move/index")
    public Result moveIndex(@SessionAttribute(SessionAttr.MS_USER) SysUser sysUser,
                            @Validated @RequestBody SysDeptMoveIndex request) {
        logger.info("组织机构移动,username:{},参数:{}", sysUser.getUsername(), request.toString());
        msSysDeptService.indexMove(request);
        return ResultUtil.success();
    }


    @RequiresPermissions(value = {"sys/deptUser/add", "sys-gscx/deptUser/add"}, logical = Logical.OR)
    @PostMapping("/user/add")
    public Result userAdd(@SessionAttribute(SessionAttr.MS_USER) SysUser sysUser,
                          @Validated @RequestBody SysDeptUserAdd request) {
        logger.info("组织机构添加用户,username:{},request:{}", sysUser.getUsername(), request.toString());
        msSysUserService.addDeptUser(request);
        return ResultUtil.success();
    }

    @RequiresPermissions(value = {"sys/emp/user/add", "sys-gscx/emp/user/add"}, logical = Logical.OR)
    @PostMapping("/emp/user/add")
    public Result empUserAdd(@SessionAttribute(SessionAttr.MS_USER) SysUser sysUser,
                          @Validated @RequestBody SysEmpUserAdd request) {
        logger.info("实名制人员关联用户,username:{},request:{}", sysUser.getUsername(), request.toString());
        msSysUserService.addEmpUser(request);
        return ResultUtil.success();
    }

    @RequiresPermissions(value = {"sys/deptUser/add", "sys-gscx/deptUser/add"}, logical = Logical.OR)
    @PostMapping("/user/import")
    public Result userImport(@SessionAttribute(SessionAttr.MS_USER) SysUser sysUser,
                             @Validated @RequestBody SysDeptUserImport request) {
        logger.info("组织机构导入已有用户,username:{},request:{}", sysUser.getUsername(), request.toString());
        msSysDeptService.importUser(request);
        return ResultUtil.success();
    }

    @RequiresPermissions(value = {"sys/deptUser/edit", "sys-gscx/deptUser/edit"}, logical = Logical.OR)
    @PostMapping("/user/edit")
    public Result userEdit(@SessionAttribute(SessionAttr.MS_USER) SysUser sysUser,
                           @Validated @RequestBody SysDeptUserEdit request) {
        logger.info("组织机构修改用户,username:{},request:{}", sysUser.getUsername(), request.toString());
        msSysUserService.editDeptUser(request);
        return ResultUtil.success();
    }


    @RequiresPermissions(value = {"sys/deptUser/remove", "sys-gscx/deptUser/remove"}, logical = Logical.OR)
    @PostMapping("/user/remove")
    public Result userRemove(@SessionAttribute(SessionAttr.MS_USER) SysUser sysUser,
                             @Validated @RequestBody SysDeptUserRemove request) {
        logger.info("组织机构移除用户,username:{},request:{}", sysUser.getUsername(), JSONUtil.toString(request));
        msSysDeptService.removeUser(request.getDeptId(), request.getUserId());
        return ResultUtil.success();
    }


    @GetMapping("/user/import/list")
    public Result userImportList(@SessionAttribute(SessionAttr.MS_USER) SysUser sysUser,
                                 @RequestParam("deptId") Integer deptId,
                                 @RequestParam("pageNum") Integer pageNum,
                                 @RequestParam("pageSize") Integer pageSize,
                                 @RequestParam(value = "keyword", required = false) String keyword) {
        logger.info("组织机构获取可导入用户,username:{},deptId:{}, pageNum:{}, pageSize:{}, keyword:{}",
                sysUser.getUsername(), deptId, pageNum, pageSize, keyword);
        PageData<SysUserDTO> pageData = msSysDeptService.getImportUserList(sysUser, deptId, pageNum, pageSize, keyword);
        return ResultUtil.success(pageData);
    }

    @GetMapping("/emp/user/import/list")
    public Result empUserImportList(@SessionAttribute(SessionAttr.MS_USER) SysUser sysUser,
                                 @RequestParam("deptId") Integer deptId,
                                 @RequestParam("pageNum") Integer pageNum,
                                 @RequestParam("pageSize") Integer pageSize,
                                 @RequestParam(value = "keyword", required = false) String keyword) {
        logger.info("实名制人员获取可关联用户,username:{},deptId:{}, pageNum:{}, pageSize:{}, keyword:{}",
                sysUser.getUsername(), deptId, pageNum, pageSize, keyword);
        PageData<SysUserDTO> pageData = msSysDeptService.getImportEmpUserList(sysUser, deptId, pageNum, pageSize, keyword);
        return ResultUtil.success(pageData);
    }

    @GetMapping("/extra")
    public Result getDeptExtra(@SessionAttribute(SessionAttr.MS_USER) SysUser sysUser,
                               @RequestParam("deptId") Integer deptId) {
        logger.info("获取组织机构扩展信息， loginer:{}, deptId:{}", sysUser.getUsername(), deptId);
        SysDeptExtraDTO deptExtra = msSysDeptService.getDeptExtra(deptId);
        return ResultUtil.success(deptExtra);
    }


    @PostMapping("/extra/set")
    public Result setDeptExtra(@SessionAttribute(SessionAttr.MS_USER) SysUser sysUser,
                               @RequestBody @Validated SysDeptExtraParam param) {
        logger.info("设置组织机构扩展信息, loginer:{}, param:{}", sysUser.getUsername(), param);
        msSysDeptService.setDeptExtra(param);
        return ResultUtil.success();
    }

    @GetMapping("/extra/customize/field")
    public Result extraCustomizeField(@SessionAttribute(SessionAttr.MS_USER) SysUser sysUser,
                                      @RequestParam("deptId") Integer deptId) {
        logger.info("获取组织机构扩展信息自定义字段， loginer:{}, deptId:{}", sysUser.getUsername(), deptId);
        String customizeField = msSysDeptService.getExtraCustomizeField(deptId);
        return ResultUtil.success(customizeField);
    }

}
